package com.sniff.atp.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sniff.atp.modules.test.mapper.MobilePhoneMapper;
import com.sniff.atp.modules.test.model.MobilePhone;
import com.sniff.atp.modules.test.model.MobilePhoneLog;
import com.sniff.atp.modules.test.service.MobilePhoneLogService;
import com.sniff.atp.modules.test.service.MobilePhoneService;
import jodd.util.StringUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.swagger2.mappers.ModelMapper;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.modules.test.service.impl
 * @Author: zxy
 * @CreateTime: 2023-11-27 13:30
 * @Description:
 * @Version: 1.0
 */
@Service
public class MobilePhoneServiceImpl extends ServiceImpl<MobilePhoneMapper, MobilePhone> implements MobilePhoneService {

    @Autowired
    private MobilePhoneLogService mobilePhoneLogService;


    @Override
    public boolean create(MobilePhone mobilePhone) {
        if (mobilePhone == null) {
            return false;
        }
        mobilePhone.setCreateTime(new Date());
        mobilePhone.setUpdateTime(new Date());
        boolean saveFlag = save(mobilePhone);

        //新增手机日志
        try {
            //查询商品
            QueryWrapper<MobilePhone> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .eq(StringUtil.isNotBlank(mobilePhone.getMobileType()),MobilePhone::getMobileType,mobilePhone.getMobileType())
                    .orderByDesc(MobilePhone::getCreateTime);
            List<MobilePhone> mobilePhoneList = list(wrapper);
            Long mobileId = 0L;
            if(mobilePhoneList.size() > 0){
                mobileId = mobilePhoneList.get(0).getMobileId();
                MobilePhoneLog mobilePhoneLog = new MobilePhoneLog();
                mobilePhoneLog.setMobileId(mobileId);
                mobilePhoneLog.setMobileType(mobilePhone.getMobileType());
                mobilePhoneLog.setMobileSystem(mobilePhone.getMobileSystem());
                mobilePhoneLog.setMobileOperateStatus(mobilePhone.getMobileOperateStatus());
                mobilePhoneLog.setMobileUsePerson(mobilePhone.getMobileUsePerson());
                mobilePhoneLog.setMobileUseDate(mobilePhone.getMobileUseDate());
                mobilePhoneLog.setRemark(mobilePhone.getRemark());
                mobilePhoneLog.setCreateTime(new Date());
                mobilePhoneLog.setUpdateTime(new Date());

                mobilePhoneLogService.create(mobilePhoneLog);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return saveFlag;
    }

    @Override
    public boolean update(MobilePhone mobilePhone) {
        mobilePhone.setUpdateTime(new Date());

        //新增手机日志
        try {
            MobilePhoneLog mobilePhoneLog = new MobilePhoneLog();
            mobilePhoneLog.setMobileId(mobilePhone.getMobileId());
            mobilePhoneLog.setMobileType(mobilePhone.getMobileType());
            mobilePhoneLog.setMobileSystem(mobilePhone.getMobileSystem());
            mobilePhoneLog.setMobileOperateStatus(mobilePhone.getMobileOperateStatus());
            mobilePhoneLog.setMobileUsePerson(mobilePhone.getMobileUsePerson());
            mobilePhoneLog.setMobileUseDate(mobilePhone.getMobileUseDate());
            mobilePhoneLog.setRemark(mobilePhone.getRemark());
            mobilePhoneLog.setCreateTime(new Date());
            mobilePhoneLog.setUpdateTime(new Date());

            mobilePhoneLogService.create(mobilePhoneLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return updateById(mobilePhone);
    }

    @Override
    public Page<MobilePhone> list(MobilePhone mobilePhone, Integer pageSize, Integer pageNum) {
        Page<MobilePhone> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MobilePhone> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StringUtil.isNotBlank(mobilePhone.getMobileType()), MobilePhone::getMobileType,mobilePhone.getMobileType())
                .like(StringUtil.isNotBlank(mobilePhone.getMobileUsePerson()),MobilePhone::getMobileUsePerson,mobilePhone.getMobileUsePerson())
                .eq(StringUtil.isNotBlank(mobilePhone.getMobileSystem()),MobilePhone::getMobileSystem,mobilePhone.getMobileSystem())
                .eq(mobilePhone.getMobileOperateStatus() != 0,MobilePhone::getMobileOperateStatus,mobilePhone.getMobileOperateStatus())
                .ge(mobilePhone.getStartTime() != null,MobilePhone::getMobileUseDate,mobilePhone.getStartTime())
                .le(mobilePhone.getEndTime() != null,MobilePhone::getMobileUseDate,mobilePhone.getEndTime())
                .orderByDesc(MobilePhone::getCreateTime);
        return page(page, wrapper);
    }
}
